package com.crop.back.controller;

import com.crop.back.common.enums.Deleted;
import com.crop.back.common.utils.ResultBean;
import com.crop.back.dto.ProcessingBatchDTO;
import com.crop.back.entity.ProcessingBatch;
import com.crop.back.entity.Product;
import com.crop.back.entity.ProductCategory;
import com.crop.back.entity.ProductRecord;
import com.crop.back.entity.QualityInspection;
import com.crop.back.service.ProcessingBatchService;
import com.crop.back.service.ProductCategoryService;
import com.crop.back.service.ProductRecordService;
import com.crop.back.service.ProductService;
import com.crop.back.service.QualityInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping("/statistics")
public class StatisticsController {


    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCategoryService categoryService;
    @Autowired
    private ProductRecordService recordService;
    @Autowired
    private ProcessingBatchService batchService;
    @Autowired
    private QualityInspectionService inspectionService;


    @GetMapping
    public ResultBean<Map<String, Long>> getStatistics() {

        Long productCount = productService.lambdaQuery()
                .eq(Product::getDeleted, Deleted.NOT_DELETED.getCode())
                .count();
        Long categoryCount = categoryService.lambdaQuery()
                .eq(ProductCategory::getDeleted, Deleted.NOT_DELETED.getCode())
                .count();
        Long recordCount = recordService.lambdaQuery()
                .eq(ProductRecord::getDeleted, Deleted.NOT_DELETED.getCode())
                .count();
        Long batchCount = batchService.lambdaQuery()
                .eq(ProcessingBatch::getDeleted, Deleted.NOT_DELETED.getCode())
                .count();
        Long inspectionCount = inspectionService.lambdaQuery()
                .eq(QualityInspection::getDeleted, Deleted.NOT_DELETED.getCode())
                .count();



        // 将统计数据封装到Map中
        Map<String, Long> statistics = Map.of(
                "productCount", productCount,
                "categoryCount", categoryCount,
                "recordCount", recordCount,
                "batchCount", batchCount,
                "inspectionCount", inspectionCount
        );

        return ResultBean.ok(statistics);
    }
}
